/*
 * @Description:
 * @Author: wlj
 * @Date: 2023-01-03 10:51:19
 * @LastEditors: wlj
 * @LastEditTime: 2023-01-03 16:32:03
 * @FilePath: \my-vite-app\src\utils\request.ts
 */

import { AxiosInstance, AxiosRequestConfig, AxiosResponse } from "axios";
import router from "../router/index";

const baseURL = import.meta.env.VITE_APP_URL as string; //根据环境获得不同的代理模式
const instance: AxiosInstance = axios.create({
  baseURL,
  timeout: import.meta.env.VITE_APP_TIMEOUT,
});

instance.interceptors.request.use(
  (config: AxiosRequestConfig) => {
    const headers = config.headers;
    // if (config.method === "post") {
    //   config.data = JSON.stringify(config.data);
    // }
    return config;
  },
  (error: any) => {
    console.log(error);
    return Promise.reject(error);
  }
);

instance.interceptors.response.use(
  (response: AxiosResponse): any => {
    if (response.status === 200) {
      return Promise.resolve(response.data);
    }
  },
  (error: any) => {
    console.log(error);
    return Promise.reject(error);
  }
);

export default instance;
